//
//  CZStatusViewModel.swift
//  weiBo
//
//  Created by apple on 16/5/14.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class CZStatusViewModel: NSObject {
    var status : CZStatus?
    //  转发微博内容
    var retweetContent: String?
    
    //  转发数内容
    var retweetCountContent: String?
    //  评论数
    var commentCountContent: String?
    //  赞
    var unlikeCountContent: String?
    
    //  来源
    var sourceContent: String?
    //会员等级图片
    var mbrankImage: UIImage?
    //认证类型图片
    var verifiedTypeImage: UIImage?
    
    // 发微博时间  定义只读属性
    var timeContent: String? {
            guard let creatAt = status?.created_at else {
                return nil
            }

            return NSDate.sinaDate(creatAt).sinaDateString

    }
    
    init(status : CZStatus){
        super.init()
        self.status = status
        handleRetWeetContent(status)
        handleSourceContent(status)
        handleMBrankImage(status.user?.mbrank ?? 0)
        handleVerifiedTypeImage(status.user?.verified_type ?? 0)
        //  转发数
        retweetCountContent = handleCountContent(status.reposts_count, defaultTile: "转发")
        //  评论数
        commentCountContent = handleCountContent(status.comments_count, defaultTile: "评论")
        //  赞数
        unlikeCountContent = handleCountContent(status.attitudes_count, defaultTile: "赞")
        
        
    }
    
    private func handleMBrankImage(count: Int) {
        if count > 0 && count < 7{
            mbrankImage = UIImage(named: "common_icon_membership_level\(count)")
        }
    }
    
    //处理认证图片
    private func handleVerifiedTypeImage(count :Int){
        switch count {
        case 0:
            verifiedTypeImage = UIImage(named: "avatar_vip")
        case 2, 3, 5:
            verifiedTypeImage = UIImage(named: "avatar_enterprise_vip")
        case 220:
            verifiedTypeImage = UIImage(named: "avatar_grassroot")
        default:
            verifiedTypeImage = nil
    
        }
        
    }
    
    //处理评论数
    private func handleCountContent(count: Int , defaultTile: String) -> String{
        if count > 0{
            if count >= 10000 {
                let result = CGFloat(count / 10000)
                let resultStr = "\(result)万"
                
                return resultStr
            }else{
                return "\(count)"
            }
            
        
        }else{
            return defaultTile
        }
    
    
    }
    
    
    //拼接数据
    private func handleRetWeetContent(status: CZStatus){
        guard let retweet = status.retweeted_status?.text else{
            return
        }
        guard let name = status.retweeted_status?.user?.screen_name else{
            return
        }
        //拼接转发微博内容
        
        retweetContent = "@\(name):\(retweet)"
        
    }
    
    //处理来源数据
    private func handleSourceContent(status: CZStatus) {
        guard let source = status.source else {
            return
        }
        if source.containsString("\">") {
            
            let startIndex = source.rangeOfString("\">")?.endIndex
            let endIndex = source.rangeOfString("</")?.startIndex
            
            if let start = startIndex, end = endIndex {
                sourceContent = source.substringWithRange(start..<end)
                
                
            }
            
        }
    
    
    
    
    
    }

    
    
    
    
    
}
